home *** CD-ROM | disk | FTP | other *** search
- From: Steve_Quist@msn.com (Stephen Quist)
- Subject: RE: Proper use of friend keyword
- Date: 6 Apr 96 06:12:14 -0800
- References: <4jn38u$j9c@holly.ACNS.ColoState.EDU>
- Message-ID: <00001a81+0000b164@msn.com>
- Path: news.msn.com!msn.com
- Newsgroups: comp.lang.c++
- Organization: The Microsoft Network (msn.com)
-
- Corby Hudnall wrote:
-
- Hey all, I have a question about how to use the friend keyword. Consider
- the following example:
-
- class.h---------
- class ABC
- {
- int AValue;
- int GetAValue() { return (AValue); }
- public:
- ABC() { AValue=5; }
- ~ABC(){ }
- };
-
- class XYZ
- {
- public:
- XYZ() { }
- ~XYZ() { }
- friend int ABC::GetAValue();
- };
-
- prog.C--------------------
- #include <iostream.h>
- #include "class.h"
-
- void main()
- {
- ABC abc;
- XYZ xyz;
-
- cout << xyz.GetAValue() << endl
- }
-
-
- when I try to compile this, I get the message "no member funciton
- 'XYZ::GetAValue()' defined." What do I need to do in order to
- make this work. Thanks for any and all suggestions.
- -----------------
- Well, the compiler is of course right. I am not sure what you are
- trying to do. If you are trying to make GetAValue() a mf of XYZ
- then ABC and XYZ should have an inheritance relationship. The
- friend relationship means that ABC::GetAValue() can access the
- private parts of an XYZ object. To flesh it out a little bit lets
- give XYZ some private data.
-
- class.h---------
- class XYZ; // forward declaration
- class ABC
- {
- int AValue;
- int GetAValue(XYZ &t) { return (AValue * t.private_member); }
- public:
- ABC() { AValue=5; }
- ~ABC(){ }
- };
-
- class XYZ
- {
- public:
- XYZ() :private_member(2) { }
- ~XYZ() { }
- friend int ABC::GetAValue();
- private:
- int private_member;
- };
-
- Now ABC::GetAValue(XYZ&) has friend access to XYZ
-
- Hope this helps
- Steve Quist
-